/**
 * Created by Administrator on 15-7-31.
 */
console = console || {
	log : function(){ return false;},
	dir : function(){ return false;}
}
var Delay = (function(){

	function Delay(){
		this.timer = null;
		return this;
	}
	Delay.prototype = {
		set : function(callback,time){
			if(typeof time !== "number") time = 1 * 1000;
			this.clear();
			this.timer = setTimeout(function(){
				callback();
			},time)
		},
		clear : function(){
			clearTimeout(this.timer);
			this.timer = null;
		}
	}
	return Delay;

})();
var Alert = function(type,content,duration,callback){
	var type  = type || "success";
	var duration = duration || 1 * 1000;
	if(!content) return false;
	var wrap = $("#gTopAlertContainer");
	if(!wrap.length){
		wrap = $('<div id="gTopAlertContainer"></div>');
		var styleTxt = '#gTopAlertContainer{ position:fixed; z-index:1000; left:50%; top:-35px; height:35px; padding:0 30px; line-height:35px; font-size:14px; color:#fff; text-align:center; background:#fff;-webkit-box-shadow:1px 1px 2px rgba(0,0,0,.3);box-shadow:1px 1px 2px rgba(0,0,0,.3);}#gTopAlertContainer p{ width:180px;}#gTopAlertContainer.success{ background:#3eba40}#gTopAlertContainer.fail{ background:#df262a}#gTopAlertContainer.error{ background:#df262a}'
		var style = $('<style type="text/css">'+styleTxt+'</style>');
		$("body").append(wrap);
		$("body").append(style);
	}
	wrap.removeClass("success").removeClass("error").removeClass("fail");
	wrap.addClass(type);
	wrap.html(content);
	setTimeout(function(){
		wrap = $("#gTopAlertContainer");
		var width = wrap.outerWidth(true);
		var height = wrap.outerHeight(true);
		wrap.css({"marginLeft":-width/2,"top":-height-2});
		wrap.animate({top:0},"normal",function(){
			var delay = new Delay();
			delay.set(function(){
				wrap.animate({top:-height-2},"normal",function(){
					callback && callback();
				})
			},duration)
		})
	},0)
}
var Pop = {
	show : function(pop){
		pop.addClass("show").show();
		$("#mask").show().css({
			zIndex : 1001,
			opacity : 0.6,
			backgroundColor : "#000"
		})
	},
	hide : function(pop){
		pop.removeClass("hide").hide();
		$("#mask").hide().css({
			zIndex : -1
		})
	}
};
var theme = new Theme({container:$("#themePopBox")});
var RichEditor = new Editor();
var Main = RichBase.extend({
	statics : {
		api : "module/pft_resource/api.php"
	},
	EVENTS : {
		"click" : {
			"#themeInp" : "onThemeInpClick",
			"#submitBtn" : "onSubmitBtnClick"
		}
	},
	init : function(){
		ImgUploador.init();
		PtypeSwitch.init();
		this.formSubmitSuccessDialog = new PFT.Dialog({
			title : "添加成功",
			onBeforeOpen : function(){ },
			onAfterOpen : function(){ },
			onBeforeClose : function(){ },
			onAfterClose : function(){ },
			content : $("#formSubmitSuccessDialogTpl").html(),
			EVENTS : {}
   	 	});
		this.prov_city_select();
	},
	prov_city_select : function(){
		var provs = PFT.Config.Provinces;
		var initArea = $("#area").children("input").val().split("|");
		var initProvid = initArea[0];
		var initCityid = initArea[1];
		var pSelect = $("#d_province");
		var cSelect = $("#d_city");
		var phtml = "";
		var getCityByProv = (function(){
			var citys = {};
			return function(provid){
				if(!provid) return false;
				if(citys[provid]) return cSelect.html(citys[provid]);
				PFT.Ajax({
					url : "call/getAreas.php?act=getCity&area_deeppath=2",
					type : "get",
					dataType : "json",
					data : {
						parentId : provid
					}
				},function(res){
					var html = "";
					for(var i in res){
						var city = res[i];
						var cityid = city["area_id"];
						var cityname = city["area_name"];
						html += '<option value="'+cityid+'" data-cityid="'+cityid+'">'+cityname+'</option>'
					}
					citys[provid] = html;
					cSelect.html(html);
					setTimeout(function(){
						if(initCityid) cSelect.val(initCityid);
					},10)
				})
			}
		})();
		for(var i in provs){
			var p = provs[i];
			var provid = p["area_id"];
			var prov = p["area_name"];
			phtml += '<option value="'+provid+'" data-provid="'+provid+'">'+prov+'</option>';
		}
		pSelect.html(phtml);
		pSelect.on("change",function(e){
			var $this = $(this);
			var val = $this.val();
			getCityByProv(val);
		})
		if(initProvid){
			pSelect.val(initProvid);
			getCityByProv(initProvid);
		}else{
			pSelect.trigger("change");
		}
	},
	onThemeInpClick : function(that,e){
		theme.show();
	},
	onSubmitBtnClick : function(that,e){
		e.stopPropagation();
		var form = $("#submitForm");
		var data = form.serialize();
		var btn = $("#submitBtn");
		if(btn.hasClass("loading")) return false;
		PFT.Ajax({
			url : that.statics.api,
			type : "post",
			dataType : "json",
			data : data,
			loading : function(){ btn.addClass("loading").text("提交中..")},
			removeLoading : function(){ btn.removeClass("loading").text("提交")},
			timeout : function(){ console.log("提交数据超时")},
			serverError : function(){ console.log("提交数据服务器出错")}
		},function(res){
			var code = res.code;
			var msg = res.msg || "保存数据失败，请稍后重试";
			if(code==200){
				//Alert("success",'<p style="width:160px;"></p>');
				that.formSubmitSuccessDialog.open({
					title : "保存数据成功",
					offset : -100
				})
			}else{
				Alert("fail",'<p style="width:260px;">'+msg+'</p>');
			}
		})
	}
});

// 2015/10/22新增资源方功能
var Resor = {
	timer : null,
	SEARCH_TIMEOUT : 200,
	init : function(){
		var that = this;
		var triggerInp = this.triggerInp = $("#resorInp");
		this.selectPop = $("#resorPage");
		this.selectUl = $("#resorUl");
		triggerInp.on("keyup",function(e){
			clearTimeout(that.timer);
			that.timer = setTimeout(function(){
				var keyword = triggerInp.val();
				that.onInputChange(keyword);
			},that.SEARCH_TIMEOUT)
		}).on("focus",function(e){
//			if($(this).val() && that.selectUl.children().length) that.showSelect();
		})
		this.selectPop.on("click",function(){
			that.closeSelect();
		})
		this.selectUl.on("click","li",function(e){
			var tarItem = $(e.currentTarget);
			var text = tarItem.text();
			var id = tarItem.attr("data-id");
			triggerInp.val(text);
			$("#applydidHidInp").val(id);
			tarItem.addClass("active").siblings().removeClass("active");
		})
		this.posUl();
	},
	onInputChange : function(keyword){
		var that = this;
		var selectUl = this.selectUl;
		this.search(keyword,{
			loading : function(){ $("#resorLoading").show()},
			removeLoading : function(){ $("#resorLoading").hide()},
			success : function(res){
				var html = "";
				for(var i in res){
					var d = res[i];
					var id = d["id"];
					var name = d["dname"];
					html += '<li class="resLi" data-id="'+id+'">'+name+'</li>';
				}
				that.showSelect();
				selectUl.html(html);
			},
			empty : function(res){ that.closeSelect()},
			fail : function(res){ that.closeSelect()},
			timeout : function(res){ that.closeSelect()},
			serverError : function(res){ that.closeSelect()}
		})
	},
	search : function(keyword,opt){
		if(!keyword) return this.closeSelect();
		var opt = opt || {};
		var fn = new Function;
		var loading = opt.loading || fn;
		var removeLoading = opt.removeLoading || fn;
		var success = opt.success || fn;
		var empty = opt.empty || fn;
		var fail = opt.fail || fn;
		var timeout = opt.timeout || fn;
		var serverError = opt.serverError || fn;
		PFT.Ajax({
			url : "call/jh_mem.php",
			type : "post",
			dataType : "json",
			data : {
				action : "fuzzyGetDname_c",
				dname : keyword,
				dtype : 0
			},
			loading : function(){ loading()},
			removeLoading : function(){ removeLoading()},
			timeout : function(res){ timeout(res)},
			serverError : function(res){ serverError(res)}
		},function(res){
			if(Object.prototype.toString.call(res)=="[object Array]"){
				if(res.length){
					success(res);
				}else{
					empty(res);
				}
			}else{
				fail(res);
			}
		})
	},
	posUl : function(){
		var triggerInp = this.triggerInp;
		var selectUl = this.selectUl;
		var offset = triggerInp.offset();
		selectUl.css({
			left : offset.left,
			top : offset.top + 36
		})
	},
	showSelect : function(){
		this.selectPop.show().css("zIndex",10000);
	},
	closeSelect : function(){
		this.selectPop.hide().css("zIndex",-1);
	}
}




$(function(){
	new Main({container:$("#mainContainer")});
	Resor.init();
})


);
})


